﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;

// The Templated Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234235

namespace douban.app.Controls
{
    public sealed class GroupViewer : Control
    {
        private ScrollViewer Part_ScrollViewer;
        private Image Part_ImageControl;
        private ItemsControl Part_ItemsControl;
        private Border Part_Border;

        public GroupViewer()
        {
            this.DefaultStyleKey = typeof(GroupViewer);
        }


        protected override void OnApplyTemplate()
        {
            Part_ScrollViewer = (ScrollViewer) GetTemplateChild(nameof(Part_ScrollViewer));
            Part_ItemsControl = (ItemsControl) GetTemplateChild(nameof(Part_ItemsControl));
            Part_Border = (Border)GetTemplateChild(nameof(Part_Border));
            Part_ScrollViewer.SizeChanged += Part_ScrollViewer_SizeChanged;
            Part_Border.SizeChanged += Part_Border_SizeChanged;

            Part_ImageControl = (Image)GetTemplateChild(nameof(Part_ImageControl));

            Part_ScrollViewer.ViewChanged += ScrollViewer_ViewChanged;
            Part_ScrollViewer.ViewChanging += ScrollViewer_ViewChanging;

            Part_ScrollViewer.DirectManipulationCompleted += Part_ScrollViewer_DirectManipulationCompleted;
            Part_ScrollViewer.DirectManipulationStarted += Part_ScrollViewer_DirectManipulationStarted;
        }

        private void Part_ScrollViewer_DirectManipulationStarted(object sender, object e)
        {
           
        }

        private void Part_ScrollViewer_DirectManipulationCompleted(object sender, object e)
        {
            
        }

        private void Part_Border_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Part_ScrollViewer.Width = Part_Border.Width;
        }

        private void Part_ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Part_ScrollViewer.Width = Part_ScrollViewer.ActualWidth;
        }

        private void ScrollViewer_ViewChanging(object sender, ScrollViewerViewChangingEventArgs e)
        {
            var finalOffset = e.FinalView.HorizontalOffset;
            var offset = Part_ScrollViewer.HorizontalOffset;

            if (finalOffset > offset)
            {
                NextIndex = Index + 1;
            }
            else
            {
                NextIndex = Index - 1;
            }
        }

        private void ScrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
        {
            if (Part_ScrollViewer.HorizontalOffset.Equals(NextIndex * ScrollViewerWidth)) return;
            Part_ScrollViewer.ViewChanging -= ScrollViewer_ViewChanging;

            Part_ScrollViewer.ScrollToHorizontalOffset(NextIndex * ScrollViewerWidth);
            Part_ScrollViewer.ViewChanging += ScrollViewer_ViewChanging;
        }

        public int Index
        {
            get { return (int)(Part_ScrollViewer.HorizontalOffset / ScrollViewerWidth); }
        }

        private int NextIndex { get; set; }

        private double ScrollViewerWidth
        {
            get
            {
                return Part_ScrollViewer.ActualWidth;
            }
        }
    }
}
